/*
 * @Author: your name
 * @Date: 2021-04-28 10:09:47
 * @LastEditTime: 2021-06-08 21:01:38
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \umi\src\models\renewal.ts
 */
import type { Reducer, Effect } from 'umi';
import {
  getRenewalList,
  renewalProductDetail,
  getRenewalLisForModal,
  importProduct,
  recycleRenewal,
  backUpRenewal,
  updateReplace,
  smsTemplates,
  updateSms,
  updateConfig,
  resetSms,
} from '@/services/renewal';
import { getPermissionByCheckKey } from '@/services/common';
import type { columnsEnum } from '@/pages/renewalPanel/data.d';
import shortid from 'shortid';

export type RenewalStateType = {
  renewalList: columnsEnum[];
  pageIndex: number;
  total: number;
  productDetail: any;
  productModalList: any;
  productModalListR: any;
  pageIndexModal: number;
  totalModal: number;
  totalModalR: number;
  pageIndexModalR: number;
  renewalListRecycle: columnsEnum[];
  totalRecycle: number;
  pageIndexRecycle: number;
  smsTemplates: any;
};

export type RenewalModelType = {
  namespace: string;
  state: RenewalStateType;
  effects: {
    getRenewalList: Effect;
    getRenewalProductDetail: Effect;
    getRenewalProductForModal: Effect;
    getRenewalProductForModalR: Effect;
    importProduct: Effect;
    recycleRenewal: Effect;
    getRenewalIsRecycleList: Effect;
    backUpRenewal: Effect;
    updateReplace: Effect;
    smsTemplates: Effect;
    updateSms: Effect;
    fakeUpdateProductDetail: Effect;
    updateConfig: Effect;
    getResetSms: Effect;
    updateAddStatus: Effect;
    getPermission: Effect;
  };
  reducers: {
    changeRenewalList: Reducer;
    changeRenewalDetail: Reducer;
    changeProductModalList: Reducer;
    changeRenewalIsRecycleList: Reducer;
    changeProductModalListR: Reducer;
    changeSmsTemplates: Reducer;
    changeFakeUpdateProductDetail: Reducer;
    changeUpdateAddStatus: Reducer;
  };
};

const Model: RenewalModelType = {
  namespace: 'renewal',

  state: {
    renewalList: [],
    pageIndex: 1,
    total: 0,
    productDetail: {},
    productModalList: [],
    pageIndexModal: 1,
    totalModal: 0,
    renewalListRecycle: [],
    totalRecycle: 0,
    pageIndexRecycle: 1,
    productModalListR: [],
    pageIndexModalR: 1,
    totalModalR: 0,
    smsTemplates: [],
  },

  effects: {
    *getRenewalList({ payload }, { call, put }) {
      const response = yield call(getRenewalList, payload);
      // eslint-disable-next-line no-console
      console.log('getRenewalList', response);
      if (response.success && response.data) {
        yield put({
          type: 'changeRenewalList',
          payload: response,
        });
      }
      return response;
    },
    *getRenewalIsRecycleList({ payload }, { call, put }) {
      const response = yield call(getRenewalList, payload);
      if (response.success && response.data) {
        yield put({
          type: 'changeRenewalIsRecycleList',
          payload: response,
        });
      }
      return response;
    },
    *getRenewalProductDetail({ payload }, { call, put }) {
      const response = yield call(renewalProductDetail, payload);
      if (response.success) {
        yield put({
          type: 'changeRenewalDetail',
          payload: response,
        });
      }
      return response;
    },
    *getRenewalProductForModal({ payload }, { call, put }) {
      const response = yield call(getRenewalLisForModal, payload);
      if (response.success) {
        yield put({
          type: 'changeProductModalList',
          payload: response,
        });
      }
      return response;
    },

    *getRenewalProductForModalR({ payload }, { call, put }) {
      const response = yield call(getRenewalLisForModal, payload);
      if (response.success) {
        yield put({
          type: 'changeProductModalListR',
          payload: response,
        });
      }
      return response;
    },

    *importProduct({ payload }, { call }) {
      const response = yield call(importProduct, payload);
      return response;
    },
    *recycleRenewal({ payload }, { call }) {
      const response = yield call(recycleRenewal, payload);
      return response;
    },
    *backUpRenewal({ payload }, { call }) {
      const response = yield call(backUpRenewal, payload);
      return response;
    },
    *updateReplace({ payload }, { call }) {
      const response = yield call(updateReplace, payload);
      return response;
    },
    *smsTemplates({ payload }, { call, put }) {
      const response = yield call(smsTemplates, payload);

      if (response.success) {
        yield put({
          type: 'changeSmsTemplates',
          payload: response,
        });
      }
      return response;
    },
    *updateSms({ payload }, { call }) {
      const response = yield call(updateSms, payload);
      return response;
    },
    *fakeUpdateProductDetail({ payload }, { put }) {
      yield put({
        type: 'changeFakeUpdateProductDetail',
        payload,
      });
      return {
        success: true,
      };
    },
    *updateConfig({ payload }, { call }) {
      const response = yield call(updateConfig, payload);
      return response;
    },

    *getResetSms({ payload }, { call }) {
      const response = yield call(resetSms, payload);
      return response;
    },
    *updateAddStatus({ payload }, { put }) {
      yield put({
        type: 'changeUpdateAddStatus',
        payload,
      });
    },
    *getPermission({ payload }, { call }) {
      const response = yield call(getPermissionByCheckKey, payload);
      return response;
    },
  },

  reducers: {
    changeRenewalList(state, { payload }) {
      // concat数据
      // 添加key

      const dataList = (payload.data.data || []).map((i: columnsEnum) => {
        return {
          ...i,
          key: i.id,
        };
      });
      return {
        ...state,
        renewalList: dataList,
        total: payload.data.total,
        pageIndex: payload.data.pageIndex,
      };
    },
    changeRenewalIsRecycleList(state, { payload }) {
      // concat数据
      // 添加key
      const dataList = (payload.data.data || []).map((i: columnsEnum) => {
        return {
          ...i,
          key: i.id,
        };
      });
      return {
        ...state,
        renewalListRecycle: dataList,
        totalRecycle: payload.data.total,
        pageIndexRecycle: payload.data.pageIndex,
      };
    },
    changeRenewalDetail(state, { payload }) {
      return {
        ...state,
        productDetail: payload.data,
      };
    },
    changeFakeUpdateProductDetail(state, { payload }) {
      const preProductDetail = state.productDetail;
      const newProductDetail = { ...preProductDetail, ...payload };
      return {
        ...state,
        productDetail: newProductDetail,
      };
    },
    changeSmsTemplates(state, { payload }) {
      return {
        ...state,
        smsTemplates: payload.data,
      };
    },
    changeProductModalList(state, { payload }) {
      const dataList = (payload.data.data || []).map((i: columnsEnum[]) => {
        return {
          ...i,
          key: shortid.generate(),
        };
      });

      return {
        ...state,
        pageIndexModal: payload.data.pageIndex,
        totalModal: payload.data.total,
        productModalList: dataList,
      };
    },
    changeProductModalListR(state, { payload }) {
      const dataLists = payload.data.data || [];
      const dataList = dataLists.map((i: columnsEnum[]) => {
        return {
          ...i,
          key: shortid.generate(),
        };
      });

      return {
        ...state,
        pageIndexModalR: payload.data.pageIndex,
        totalModalR: payload.data.total,
        productModalListR: dataList,
      };
    },
    changeUpdateAddStatus(state, { payload }) {
      return {
        ...state,
        productModalList: payload,
      };
    },
  },
};

export default Model;
